﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Nsf.Samples.RetryActivitySample.Infrastructure.Common.Commands;
using Nsf.Samples.RetryActivitySample.Infrastructure.InternalInterfaces;
using Nsf.Samples.RetryActivitySample.WorkflowBase.Activities;

namespace Nsf.Samples.RetryActivitySample.Workflows
{
    public sealed partial class AutoManualRetryWorkflow : ControlSequentialWorkflowActivity
    {
        public Int32 SomeValue { get; set; }


        public AutoManualRetryWorkflow()
        {
            InitializeComponent();
        }

        private void GetData_ExecuteCode(object sender, EventArgs e)
        {
            if (!(sender is ActivityExecutionContext))
                return;
            var activityExecutionContext = (ActivityExecutionContext)sender;
            var logger = activityExecutionContext.GetService<ILogger>();

            if (SomeValue == 0)
                throw new ApplicationException("Value of the SomeValue must be greater then 0");
            logger.Info("SomeValue = " + SomeValue);
        }

        private void AutoManualRetryWorkflow_CommandReceived(object sender, CommandReceivedEventArgs e)
        {
            if (e.Data is SetPropertyValueCommandData)
            {
                var commandData = (SetPropertyValueCommandData)e.Data;
                if (String.Equals(commandData.PropertyName, "SomeValue", StringComparison.InvariantCultureIgnoreCase))
                    if (commandData.Value is Int32)
                        SomeValue = (Int32)commandData.Value;
            }
        }
    }

}
